// Chris and Josh made this
#include "WindowManager.h"
#include "SFML\Window.hpp"
#include "Factory.h"
#include "SFML\Graphics.hpp"
#include "SFML\Audio.hpp"
#include "TextureMap.h"
#include "Boulder.h"
#include "Log.h"
#include "Tiger.h"
#include "Hunter.h"
#include <Windows.h>
#include <iostream>

using namespace sf;
sf::Clock pauseTimer;
sf::Clock objectSpawn;
sf::Clock endGame;

float backgroundSpeed = 4.5f;

bool lastWasALog = false;
bool caughtTiger = false;
bool playingGame = false;
bool postGame = false;

int main()
{
	

	for (int i = 0; i < arrayCount; ++i)
	{
		TT::LoadTexture(arrayText[i]);
	}

	#undef CreateWindow
	WindowMan::CreateWindow(1024, 400, "THE HUNT");
	TigerObject* myTiger = TigerObject::Create();
	HunterObject* myHunter = HunterObject::Create();


	// Chris
	Sprite backgroundSprite(*TT::TextureLibrary["images/jungleBackGround.png"]);
	Sprite backgroundSprite1(*TT::TextureLibrary["images/jungleBackGround.png"]);
	backgroundSprite1.setPosition(1024.0f, 0.0f);
	// 

	Sprite menuSprite(*TT::TextureLibrary["images/main_menu_caveman.png"]);
	menuSprite.setPosition(0.0f, 0.0f);

	while (WindowMan::GetWindow()->isOpen())
	{
		sf::Event ev;
		while (WindowMan::GetWindow()->pollEvent(ev))
		{
			if (ev.type == sf::Event::Closed)
			{
				WindowMan::GetWindow()->close();
				break;
			}
		}

		// Chris
		backgroundSprite.setPosition(backgroundSprite.getPosition().x - backgroundSpeed, 0);
		backgroundSprite1.setPosition(backgroundSprite1.getPosition().x - backgroundSpeed, 0);

		if (backgroundSprite.getPosition().x < -1024.0f)
		{
			backgroundSprite.setPosition(1024.0f, 0);
		}
		if (backgroundSprite1.getPosition().x < -1024.0f)
		{
			backgroundSprite1.setPosition(1024.0f, 0);
		}
		//

		// Josh
		float objectTimer = objectSpawn.getElapsedTime().asSeconds();

		if (objectTimer > 1.5f && lastWasALog)
		{
			int random = rand() % 2;
			if (random == 1) lastWasALog = true;
			if (random == 0) lastWasALog = false;
			BoulderObject::Create();
			objectSpawn.restart();
		}

		else if (objectTimer > 1.5f && !lastWasALog)
		{
			int random = rand() % 2;
			if (random == 1) lastWasALog = true;
			if (random == 0) lastWasALog = false;
			LogObject::Create();
			objectSpawn.restart();
		}
		// 

		if (!playingGame)
		{
			WindowMan::GetWindow()->clear();
			WindowMan::GetWindow()->draw(menuSprite);
			WindowMan::GetWindow()->display();

			if (Mouse::isButtonPressed(Mouse::Button::Left))
			{
				if (Mouse::getPosition(*WindowMan::GetWindow()).x > 480 && Mouse::getPosition(*WindowMan::GetWindow()).x < 560)
				{
					if (Mouse::getPosition(*WindowMan::GetWindow()).y > 256 && Mouse::getPosition(*WindowMan::GetWindow()).y < 280)
					{
						playingGame = true;
					}

					if (Mouse::getPosition(*WindowMan::GetWindow()).y > 284 && Mouse::getPosition(*WindowMan::GetWindow()).y < 312)
					{
						WindowMan::GetWindow()->close();
					}
				}
			}
		}

		if (playingGame)
		{
			Factory::UpdateAll();
			Factory::GetDistance(myTiger->tigerSprite, myHunter->hunterSprite);
			Factory::DoCollisions();
			WindowMan::GetWindow()->clear();
			WindowMan::GetWindow()->draw(backgroundSprite);
			WindowMan::GetWindow()->draw(backgroundSprite1);
			Factory::RenderAll();
			WindowMan::GetWindow()->display();

			float endTimer = endGame.getElapsedTime().asSeconds();

			if (caughtTiger && !postGame)
			{
				endGame.restart();
				postGame = true;
			}

			if (isDead && !postGame)
			{
				postGame = true;
				endGame.restart();
			}

			if (postGame)
			{
				if (endTimer > 6.0f) playingGame = false;
			}
		}

		// Josh
		do
		{
			Sleep(0);

		} while (pauseTimer.getElapsedTime().asSeconds() < 1.f / 60.f);

		pauseTimer.restart();
		//
	}

	WindowMan::DeleteWindow();
}